﻿using System;
using HVACWebService.Common;
using HVACWebService.DomainModel.WeiXin;

namespace HVACWebService.Cloud.WeChat
{
    public class WeiXinOAuth
    {
        /// <summary>
        /// 获取HtmlOAuth验证地址
        /// </summary>
        /// <param name="html5Url"></param>
        /// <param name="appId"></param>
        /// <returns></returns>
        public string OauthUrl(string html5Url,string appId)
        {

            var url =
                "https://open.weixin.qq.com/connect/oauth2/authorize" +
                "?appid=" + appId + "" +
                "&redirect_uri=" + html5Url + "" +
                "&response_type=code" +
                "&scope=snsapi_base" +
                "&state=200" +
                "#wechat_redirect";
            return url;
        }

        /// <summary>
        /// 获取微信Code
        /// </summary>
        /// <param name="appId"></param>
        /// <param name="appSecret"></param>
        /// <param name="redirectUrl"></param>
        public string GetWeiXinCode(string appId, string appSecret, string redirectUrl)
        {
            Random r = new Random();
            //微信登录授权
            //string url = "https://open.weixin.qq.com/connect/qrconnect?appid=" + appId + "&redirect_uri=" + redirectUrl +"&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect";
            //微信OpenId授权
            //string url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appId + "&redirect_uri=" + redirectUrl +"&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect";
            //微信用户信息授权
            string url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appId + "&redirect_uri=" + redirectUrl + "&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
            return url;
        }
        /// <summary>
        /// 通过code获取access_token
        /// </summary>
        /// <param name="appId"></param>
        /// <param name="appSecret"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public WeiXinAccessTokenResultModel GetWeiXinAccessToken(string appId, string appSecret, string code)
        {
            string url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appId + "&secret=" + appSecret +
                         "&code=" + code + "&grant_type=authorization_code";
            var httpRequestTools = new HttpRequestTools();
            string jsonStr = httpRequestTools.GetHttpRequest(url);
            WeiXinAccessTokenResultModel result = new WeiXinAccessTokenResultModel();
            if (jsonStr.Contains("errcode"))
            {
                WeiXinErrorMsg errorResult = new WeiXinErrorMsg();
                errorResult = JsonSerializer.Instance.Deserialize<WeiXinErrorMsg>(jsonStr);
                result.ErrorResult = errorResult;
                result.Result = false;
            }
            else
            {
                WeiXinAccessTokenModel model = new WeiXinAccessTokenModel();

                model = JsonSerializer.Instance.Deserialize<WeiXinAccessTokenModel>(jsonStr);
                result.SuccessResult = model;
                result.Result = true;
            }
            return result;
        }
       
    }
}
